#include <stdio.h>
#include "../sjjg/str.h"

int getSel() {
    printf("1.append\n");
    printf("2.insert\n");
    printf("3.resize\n");
    printf("4.reserve\n");
    printf("5.show string\n");
    printf("6.remove\n");
    printf("7.clear\n");
    printf("8.front & back\n");
    printf("9.pop back\n");
    printf("10.sub\n");
    printf("11.replace\n");
    printf("other exit\n");
    printf("select please:");
    int i;
    scanf("%d", &i);
    return i;
}
size_t getV(char* txt, size_t len) {
    printf("enter a string:");
    scanf("%s", txt);
    return strlen(txt);
}
size_t getWhere(const char* str) {
    unsigned long i = 0;
    printf(str);
    scanf("%lu", &i);
    return i;
}

int main(int args, char* argv[]) {
    //char a[10] = "01234567";
    //memmove(a, a+3, strlen(a+3));

    //printf("%s\n", a);
    ms_string s = ms_string_create_c(NULL, 16, 17, 'N');
    if (s) {
        do
        {
            size_t w = 0;
            size_t c = 0;
            char txt[256] = "";
            int sel = getSel();
            switch (sel) {
            case 1:
                getV(txt, sizeof(txt));
                ms_string_append(s, txt, 0);
                break;
            case 2:
                getV(txt, sizeof(txt));
                w = getWhere("enter a pos:");
                ms_string_insert(s, w, txt, 0);
                break;
            case 3:
                w = getWhere("enter a size:");
                ms_string_resize(s, w);
                printf("string size:%lu,max_size:%lu\n", s->count, s->max_count);
                break;
            case 4:
                w = getWhere("enter a reserve size:");
                ms_string_reserve(s, w);
                printf("string size:%lu,max_size:%lu\n", s->count, s->max_count);
                break;
            case 5:
                printf("%lu,%lu,%s(%lu)\n",s->count,s->max_count, ms_string_data(s), strlen(s->ptr));
                break;
            case 6:
                w = getWhere("enter a pos:");
                c = getWhere("len:");
                ms_string_remove(s, w, c);
                break;
            case 7:
                ms_string_clear(s);
                break;
            case 8:
                printf("f&b:%c %c\n", ms_string_front(s), ms_string_back(s));
                break;
            case 9:
                ms_string_pop_back(s);
                break;
            case 10: {
                w = getWhere("enter a pos:");
                c = getWhere("len:");
                ms_string s2 = ms_string_sub(s,w, c);
                if (s2) {
                    printf("%lu,%lu,%s(%lu)\n", s2->count,s2->max_count,s2->ptr, strlen(s2->ptr));
                }

            }
                break;
            case 11:
                w = getWhere("enter a pos:");
                getV(txt, sizeof(txt));
                ms_string_replace(s, w, strlen(txt), txt, 0);
                break;
            default:
                goto exit;
            }
        } while (1);
exit:
        ms_string_destory(s);
    }
    return 0;
}
